layui.use(['form','layer'],function(){
	
	var form = layui.form,layer=layui.layer;
	
	
	initTree();
	
	
	$("#save-btn").on('click',function(){
		var nodes=$("#tree").jstree("get_checked");
		if(!nodes){
			layer.alert("请选择控件", {
				icon: 5,
				title: "错误"
				});
			return false;
		}
		
		var id = nodes[0];
		var text = $("#tree").jstree('get_node',id).text;
		var index = $("#index").val();
		if(isBlank(index)){
			layer.alert("页面传值失败，请重新打开页面", {
				icon: 5,
				title: "错误"
				});
			return false;
		}
		
		//判断是条件输入还是期望输出
		var selectType = $("#selectType").val();
		if(selectType == 2){
			parent.$("#dg>tr:eq("+index+")").find("input[name='operateControlId']").val(id);
			parent.$("#dg>tr:eq("+index+")").find("input[name='control-input-name']").val(text);
		}else{
			parent.$("#dg>tr:eq("+index+")").find("input[name='outputData']").val(id);
			parent.$("#dg>tr:eq("+index+")").find("input[name='data-output']").val(text);
		}
		var idx = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(idx);
	});
	
	
	$("#param").on('keydown',function(e){
		if(e.keyCode==13){
			query();
		}
	});
	
	function query(){

		
		var param = $("#param").val();
		var appId = $("#appId").val();
		var types = $("#types").val();
		var locateTypes = $("#locateTypes").val();
		
		var data = {'appId':appId,'param':param,'types':showStr(types),'locateTypes':showStr(locateTypes)};
		ajaxPost('../obj/getObjectTreeByParamWithState',data,
				function(data){
					if(data && data.data.length){
						 $("#tree").jstree(true).settings.core.data = data.data;
					 var controlId = $("#controlId").val();
					 if(!isBlank(controlId)){
						 $("#tree").jstree('select_node',controlId);
					 }
				}else
					 $("#tree").jstree(true).settings.core.data = [];
			$("#tree").jstree(true).refresh();
					 
		},function(data){
			layer.alert(data.errorMsg, {
			icon: 5,
			title: "错误"
			});
		
		});
		
	
	}
	
	
	function initTree(){
		
		var param = $("#param").val();
		var appId = $("#appId").val();
		var types = $("#types").val();
		var locateTypes = $("#locateTypes").val();
		var data = {'appId':appId,'param':param,'types':types,'locateTypes':showStr(locateTypes)};
		ajaxPost('../obj/getObjectTreeByParamWithState',data,
				function(data){
					if(data && data.data.length){
					 createObjectTree(data.data);
					 var controlId = $("#controlId").val();
					 if(!isBlank(controlId)){
						 $("#tree").jstree('select_node',controlId);
					 }
				}
					 
		},function(data){
			layer.alert(data.errorMsg, {
			icon: 5,
			title: "错误"
			});
		
		});
		
	}
	
	

	function createObjectTree(data){
		$('#tree').jstree({
			'core' : {
					'data' : data,
					'multiple':false
				},
			'checkbox' : {  
	                // 禁用级联选中  
	                'three_state' : false,       
	                'cascade' : 'undetermined' //有三个选项，up, down, undetermined; 使用前需要先禁用three_state  
	            },  
			'plugins':["checkbox","wholerow"],
			});
	}
	
});